package gspushmq.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * GGFramework 日志
 * 
 * @version 1.0
 * @since JDK1.7
 * @author Albert
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2014 SunTime Co'Ltd Inc. All rights reserved.
 * @date 2014年2月19日 下午5:11:07
 */
public class GGLogger {
	
	private static Logger debugLog = LoggerFactory.getLogger("RollingDebug"); 
	private static Logger infoLog = LoggerFactory.getLogger("Rolling"); 
	private static Logger warnLog = LoggerFactory.getLogger("RollingWarn"); 
	private static Logger errorLog = LoggerFactory.getLogger("RollingError"); 

	public static void trace(Class<?> clazz, Object obj) {
		if (obj instanceof Throwable) {
			trace(clazz, (Throwable) obj, "");
		}
		debugLog.trace((clazz == null ? "" : clazz.getName() + ": ") + (obj == null ? null : obj.toString()));
	}
	
	public static void trace(Class<?> clazz, String message, Object... args) {
		debugLog.trace(format(clazz, message, args));
	}
	
	public static void trace(Class<?> clazz, Throwable e, String message, Object... args) {
		debugLog.trace(format(clazz, message, args) + "  " + getStackTrace(e));
	}
	
//	public static void record(String message, Object... args) {
//		infoLog.info(format(null, message, args));
//	}
//	public static void record(Throwable e, String message){
//		infoLog.info(format(null, message, getStackTrace(e)));
//	}
	
	public static void record(Throwable e, StringBuffer message){
		if(null != message){
			infoLog.info("record:" + message.append(getStackTrace(e)).toString());
		}
	}
	public static void record(StringBuffer sb_message) {
		if(null != sb_message){
			infoLog.info("record:" + sb_message.toString());
		}
	}
	
	public static void debug(Class<?> clazz, Object obj) {
		if (obj instanceof Throwable) {
			debug(clazz, (Throwable) obj, "");
		}
		debugLog.debug((clazz == null ? "" : clazz.getName() + ": ") + (obj == null ? null : obj.toString()));
	}
	
	public static void debug(Class<?> clazz, String message, Object... args) {
		debugLog.debug(format(clazz, message, args));
	}
	
	public static void debug(Class<?> clazz, Throwable e, String message, Object... args) {
		debugLog.debug(format(clazz, message, args) + "  " + getStackTrace(e));
	}
	
	public static void info(Class<?> clazz, Object obj) {
		if (obj instanceof Throwable) {
			info(clazz, (Throwable) obj, "");
		}
		infoLog.info((clazz == null ? "" : clazz.getName() + ": ") + (obj == null ? null : obj.toString()));
	}
	
	public static void info(Class<?> clazz, String message, Object... args) {
		infoLog.info(format(clazz, message, args));
	}
	
	public static void info(Class<?> clazz, Throwable e, String message, Object... args) {
		infoLog.info(format(clazz, message, args) + "  " + getStackTrace(e));
	}
	
	public static void warn(Class<?> clazz, Object obj) {
		if (obj instanceof Throwable) {
			warn(clazz, (Throwable) obj, "");
		}
		warnLog.warn((clazz == null ? "" : clazz.getName() + ": ") + (obj == null ? null : obj.toString()));
	}
	
	public static void warn(Class<?> clazz, String message, Object... args) {
		warnLog.warn(format(clazz, message, args));
	}
	
	public static void warn(Class<?> clazz, Throwable e, String message, Object... args) {
		warnLog.warn(format(clazz, message, args) + "  " + getStackTrace(e));
	}
	
	
	public static void error(Class<?> clazz, Object obj) {
		if (obj instanceof Throwable) {
			error(clazz, (Throwable) obj, "");
		}
		errorLog.error((clazz == null ? "" : clazz.getName() + ": ") + (obj == null ? null : obj.toString()));
	}
	
	public static void error(Class<?> clazz, String message, Object... args) {
		errorLog.error(format(clazz, message, args));
	}
	
	public static void error(Class<?> clazz, Throwable e, String message, Object... args) {
		errorLog.error(format(clazz, message, args) + "  " + getStackTrace(e));	
	}
	
	private static String format(Class<?> clazz, String message, Object... args) {
		if (args == null || args.length == 0) {
			return (clazz == null ? "" : clazz.getName() + ": ") + message;
		}
		return String.format((clazz == null ? "" : clazz.getName() + ": ") + message, args);
	}
	
	private static String getStackTrace(Throwable t) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw, true);
		t.printStackTrace(pw);
		pw.flush();
		pw.close();
		sw.flush();
		String trace = sw.toString();
		try {
			sw.close();
		} catch (IOException e) {
			debugLog.info(e.getMessage());
		}
		if (StringUtils.isNotBlank(trace)) {
			trace = trace.replaceAll("\\r\\n\\t", ";  ");
			trace = trace.replaceAll("\\r\\n", ";  ");
			trace = trace.replaceAll("[\\t]", "	");
		}
		return trace;
	}
	
}
